package standart;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class XMLParse {

	public List ParseForConnection(String file) throws SAXException,
			IOException, ParserConfigurationException {
		List<String> list = new ArrayList<>();
		DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = f.newDocumentBuilder();
		Document doc = builder.parse(new File(file));

		NodeList nodeList = doc.getChildNodes();

		for (int i = 0; i < nodeList.getLength(); i++) {

			Node node = nodeList.item(i);

			Element eElement = (Element) node;

			if (node.getNodeName().equals("CertificateCenterConnections")
					&& node.getNodeType() == Node.ELEMENT_NODE) {

				// System.out.println(eElement.getElementsByTagName("ip").item(0).getTextContent());
				// System.out.println(eElement.getElementsByTagName("port").item(0).getTextContent());

				list.add(eElement.getElementsByTagName("ip").item(0)
						.getTextContent());
				list.add(eElement.getElementsByTagName("port").item(0)
						.getTextContent());

			}

			if (node.getNodeName().equals("DatabaseConfig")
					&& node.getNodeType() == Node.ELEMENT_NODE) {

				list.add(eElement.getElementsByTagName("url").item(0)
						.getTextContent());
				list.add(eElement.getElementsByTagName("user").item(0)
						.getTextContent());
				list.add(eElement.getElementsByTagName("password").item(0)
						.getTextContent());
			}
		}
		return list;
	}
}
